﻿using System;
using EnsureWeb.UI.Core;
using EnsureWeb.Web.DomainService;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;

namespace EnsureWeb.UI.ViewModels
{
    public class EditableViewModel : MyViewModelBase
    {
        /// <summary>
        /// Singleton for EnsureWebDomainContext
        /// </summary>
        public virtual EnsureWebDomainContext DomainContext
        {
            get
            {
                return EnsureWebDomainContext.Instance;
            }
        }

        /// <summary>
        /// Popup editable panel
        /// </summary>
        protected bool isShowDataFormPanel;
        public virtual bool IsShowDataFormPanel
        {
            get
            {
                return isShowDataFormPanel;
            }
            set
            {
                isShowDataFormPanel = value;
                RaisePropertyChanged("IsShowDataFormPanel");
            }
        }

        /// <summary>
        /// Clicking edit button
        /// </summary>
        protected ICommand itemEdit;
        public virtual ICommand ItemEdit
        {
            get
            {
                if (itemEdit == null)
                    itemEdit = new RelayCommand(() =>
                    {
                        this.IsShowDataFormPanel = true;
                    });

                return itemEdit;
            }
        }

        /// <summary>
        /// Clicking add button
        /// </summary>
        protected ICommand itemAdd;
        public virtual ICommand ItemAdd
        {
            get
            {
                if (itemAdd == null)
                    itemAdd = new RelayCommand<DataForm>(dataForm =>
                    {
                        this.IsShowDataFormPanel = true;
                        dataForm.AddNewItem();
                    });

                return itemAdd;
            }
        }

        /// <summary>
        /// Clicking delete button
        /// </summary>
        protected ICommand itemDelete;
        public virtual ICommand ItemDelete
        {
            get
            {
                if (itemDelete == null)
                    itemDelete = new RelayCommand<DataForm>(dataForm =>
                    {
                        if (dataForm.DeleteItem())
                            this.DomainContext.SubmitChanges();
                    });

                return itemDelete;
            }
        }

        /// <summary>
        /// When clicking "Ok" button on editable panel
        /// </summary>
        protected ICommand submitChanges;
        public virtual ICommand SubmitChanges
        {
            get
            {
                if (submitChanges == null)
                    submitChanges = new RelayCommand(() =>
                    {
                        this.DomainContext.SubmitChanges();
                    });

                return submitChanges;
            }
        }

        /// <summary>
        /// When clicking "Cancel" button on editable panel
        /// </summary>
        protected ICommand rejectChanges;
        public virtual ICommand RejectChanges
        {
            get
            {
                if (rejectChanges == null)
                    rejectChanges = new RelayCommand(() =>
                    {
                        this.DomainContext.RejectChanges();
                    });

                return rejectChanges;
            }
        }

        /// <summary>
        /// Initialize
        /// </summary>
        public override void Init()
        {
        }
    }
}
